package com.android.test;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class HelloWorld extends Activity {
 
	Button button;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		addListenerOnButton();
 
	}
 
	public void addListenerOnButton() {
 
		button = (Button) findViewById(R.id.button1);
 
		button.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View arg0) {
				Toast.makeText(HelloWorld.this, "Pressed, please wait", Toast.LENGTH_SHORT).show();

		    	  try {
		            // http://www.3s-solutions.gr/carassistant/middle.php

		            // Log.i(getClass().getSimpleName(), "send  task - start");
		            HttpParams httpParams = new BasicHttpParams();
		            int TIMEOUT_MILLISEC = 1000;
					HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
		            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
		            //
		            HttpParams p = new BasicHttpParams();
		            // p.setParameter("name", pvo.getName());
		            p.setParameter("user", "1");

		            // Instantiate an HttpClient
		            HttpClient httpclient = new DefaultHttpClient(p);
		            String url = "http://www.3s-solutions.gr/carassistant/" + 
		                         "middle.php?last=1&format=json";
		            HttpPost httppost = new HttpPost(url);

		            // Instantiate a GET HTTP method
		            try {
		                Log.i(getClass().getSimpleName(), "send  task - start");
		                //
		                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
		                        2);
		                nameValuePairs.add(new BasicNameValuePair("id", "1"));
		                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		                ResponseHandler<String> responseHandler = new BasicResponseHandler();
		                String responseBody = httpclient.execute(httppost,
		                        responseHandler);
//		                // Parse
//		                JSONObject json = new JSONObject(responseBody);
//		                JSONArray jArray = json.getJSONArray("posts");
//		                ArrayList<HashMap<String, String>> mylist = 
//		                       new ArrayList<HashMap<String, String>>();
		                
		         //       JSONObject jsonObj = new JSONObject(responseBody);
		                
		                
		         //     jsonObj.getJSONArray("data"); 
		              		                
		                
		                
		                JSONArray data = new JSONArray(responseBody); 
		                for(int i=0; i<data.length(); i++)
		                {
		                     JSONObject obj=data.getJSONObject(i);
		                     String id = obj.getString("ID"); 
		                        String value = obj.getString("Name"); 
		                        Log.d("Item name: ", value);
		                }
		                
		                
		                

		                /*for (int i = 0; i < jArray.length(); i++) {
		                    HashMap<String, String> map = new HashMap<String, String>();
		                    JSONObject e = jArray.getJSONObject(i);
		                    String s = e.getString("post");
		                    JSONObject jObject = new JSONObject(s);

		                    map.put("ID", jObject.getString("ID"));
		                    map.put("Name", jObject.getString("Name"));
		                    map.put("Address", jObject.getString("Address"));

		                    mylist.add(map);
		                }*/
		                Toast.makeText(HelloWorld.this, responseBody, Toast.LENGTH_LONG).show();

		            } catch (ClientProtocolException e) {
		                // TODO Auto-generated catch block
		                e.printStackTrace();
		            } catch (IOException e) {
		                // TODO Auto-generated catch block
		                e.printStackTrace();
		            }
		            // Log.i(getClass().getSimpleName(), "send  task - end");

		        } catch (Throwable t) {
		            Toast.makeText(HelloWorld.this, "Request failed: " + t.toString(),
		                    Toast.LENGTH_LONG).show();
		        }

		    	
				
			 /* Intent browserIntent = 
                            new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.3s-solutions.gr/carassistant/middle.php?last=2&format=json"));
			    startActivity(browserIntent);*/
 
			}
 
		});
 
	}
 


// pasted to here.




















//package com.android.test;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//
//import org.apache.http.NameValuePair;
//import org.apache.http.client.ClientProtocolException;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.ResponseHandler;
//import org.apache.http.client.entity.UrlEncodedFormEntity;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.impl.client.BasicResponseHandler;
//import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.message.BasicNameValuePair;
//import org.apache.http.params.BasicHttpParams;
//import org.apache.http.params.HttpConnectionParams;
//import org.apache.http.params.HttpParams;
//import org.json.JSONArray;
//import org.json.JSONObject;
//
//import android.app.Activity;
//import android.os.Bundle;
//import android.util.Log;
//import android.view.View;
//import android.widget.Button;
//import android.widget.Toast;
//
//public class HelloWorld extends Activity {
//    /** Called when the activity is first created. */
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
//        Button Button01 = (Button) findViewById(R.id.Button01);
//                        	          
//    }
    /*public void clickbutton(View v) {
    	  try {
            // http://www.3s-solutions.gr/carassistant/middle.php

            // Log.i(getClass().getSimpleName(), "send  task - start");
            HttpParams httpParams = new BasicHttpParams();
            int TIMEOUT_MILLISEC = 1000;
			HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
            //
            HttpParams p = new BasicHttpParams();
            // p.setParameter("name", pvo.getName());
            p.setParameter("user", "1");

            // Instantiate an HttpClient
            HttpClient httpclient = new DefaultHttpClient(p);
            String url = "http://www.3s-solutions.gr/carassistant/" + 
                         "middle.php?last=1&format=json";
            HttpPost httppost = new HttpPost(url);

            // Instantiate a GET HTTP method
            try {
                Log.i(getClass().getSimpleName(), "send  task - start");
                //
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        2);
                nameValuePairs.add(new BasicNameValuePair("id", "1"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String responseBody = httpclient.execute(httppost,
                        responseHandler);
                // Parse
                JSONObject json = new JSONObject(responseBody);
                JSONArray jArray = json.getJSONArray("posts");
                ArrayList<HashMap<String, String>> mylist = 
                       new ArrayList<HashMap<String, String>>();

                for (int i = 0; i < jArray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject e = jArray.getJSONObject(i);
                    String s = e.getString("post");
                    JSONObject jObject = new JSONObject(s);

                    map.put("ID", jObject.getString("ID"));
                    map.put("Name", jObject.getString("Name"));
                    map.put("Address", jObject.getString("Address"));

                    mylist.add(map);
                }
                Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Log.i(getClass().getSimpleName(), "send  task - end");

        } catch (Throwable t) {
            Toast.makeText(this, "Request failed: " + t.toString(),
                    Toast.LENGTH_LONG).show();
        }

    	}
    */
	}
